package com.zs.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.zs.common.config.ResponseCode;
import com.zs.mapper.ResourceMapper;
import com.zs.model.ResourceEntity;
import com.zs.model.ResponseCodeEntity;
import com.zs.model.criteria.BaseCriteria;
import com.zs.service.ResourceService;

/**
 * 
 * @author THINK
 *
 */
@Service("resourceServiceImpl")
public class ResourceServiceImpl implements ResourceService{

    @Resource
    private ResourceMapper resourceMapper;
    
    @Resource
    private Gson gson;
    //查询所有的资源带有用户的标志
	public String selectResourceLinkFlagWithUserId(String userId,BaseCriteria baseCriteria) {
		List<ResourceEntity> selectResourceList = resourceMapper.selectResourceListByUserId(userId);
		List<ResourceEntity> totalResult = resourceMapper.selectResourceList(baseCriteria);
		for (ResourceEntity parent : totalResult) {
			for (ResourceEntity sub : selectResourceList) {
				if(parent.getId().equals(sub.getId())){
					parent.setLinkFlag(1);
						System.out.println("ssss");
						break;
				}
			}
		}
		
    	int orderCount=resourceMapper.getTotalCount();
    	ResponseCodeEntity responseCodeTO =new ResponseCodeEntity(ResponseCode.DATA_OK,ResponseCode.DATA_SUCESS, totalResult, orderCount, baseCriteria.getPageSize(), baseCriteria.getPageNo());
    	return gson.toJson(responseCodeTO);
	}

	@Override
	public String selectResourceList(BaseCriteria baseCriteria) {
		List<ResourceEntity> selectResourceList = resourceMapper.selectResourceList(baseCriteria);
    	int orderCount=resourceMapper.getTotalCount();
    	ResponseCodeEntity responseCodeTO =new ResponseCodeEntity(ResponseCode.DATA_OK,ResponseCode.DATA_SUCESS, selectResourceList, orderCount, baseCriteria.getPageSize(), baseCriteria.getPageNo());
    	return gson.toJson(responseCodeTO);
	}

	@Override
	public String selectResourceListByUserId(String id) {
		List<ResourceEntity> selectResourceListByUserId = resourceMapper.selectResourceListByUserId(id);
		ResponseCodeEntity responseCodeTO =new ResponseCodeEntity(selectResourceListByUserId);
		return gson.toJson(responseCodeTO);
	}

	@Override
	public String addLinkUserAndResource(String userId, String resourceId) {
		resourceMapper.addLinkUserAndResource(userId, resourceId);
		ResponseCodeEntity responseCodeTO =new ResponseCodeEntity("");
		return gson.toJson(responseCodeTO);
		
	}

	@Override
	public String delLinkUserAndResource(String userId, String resourceId) {
		resourceMapper.delLinkUserAndResource(userId, resourceId);
		ResponseCodeEntity responseCodeTO =new ResponseCodeEntity("");
		return gson.toJson(responseCodeTO);
		
	}
	
    

	


}
